﻿using EaseDapper.Interface;
using System.Linq.Expressions;
using System.Collections.Concurrent;

namespace EaseDapper.Providers.ExpressionContentProvider
{
    public class ContentProvider
    {
        private readonly ConcurrentDictionary<string, IBuilder> builders = new ConcurrentDictionary<string, IBuilder>();

        public ContentProvider() { }

        internal string Builder(Expression expression, string builderType)
        {
            IBuilder builder = GetBuilder(builderType);
            builder.Resolver(expression, true);
            return builder.BuilderData();
        }

        private IBuilder GetBuilder(string builderType)
        {
            if (builders.TryGetValue(builderType, out var value))
            {
                return value;
            }
            IBuilder builder = InstanceFactory.CreateBuilderExpressions(builderType);
            builders.TryAdd(builderType, builder);
            return builder;
        }
    }
}
